﻿#include "DrawParam.h"
#include <QDebug>

DrawParam::DrawParam(QObject *parent)
    : QObject{parent}
{

}

void DrawParam::initTransform(QRect rc)
{
    rect = rc;
    offsetX = offsetY = 0;
    angle = 0.0;
    flip = 0;

    transform.reset();
    imgTrans.reset();

    float fx,fy;
    fx = rc.width()*0.98 / width;
    fy = rc.height()*0.98 / height;
    zoom = fmin(fx, fy);

    applyTransform();

}

void DrawParam::initWindow(double wl, double ww)
{
    defWL = curWL = wl;
    defWW = curWW = ww;
}

void DrawParam::applyTransform()
{
    double dx, dy;
    dx = (rect.width() - width * zoom) / 2 + rect.left();
    dy = (rect.height() - height * zoom) / 2 + rect.top();

    QPointF center;
    center.rx() = width/2.0;
    center.ry() = height/2.0;

    transform.reset();
    transform.translate(offsetX+dx, offsetY+dy);
    transform.scale(zoom, zoom);

    transform.translate(center.x(), center.y());
    transform.rotate(angle);
    transform.translate(-center.x(), -center.y());


    imgTrans.reset();
    if (flip == 1) {
        imgTrans.translate(width, 0);
        imgTrans.scale(-1, 1);
        imgTrans.translate(-width, 0);
    } else if (flip == 2) {
        imgTrans.translate(0, height);
        imgTrans.scale(1, -1);
        imgTrans.translate(0, -height);
    } else if (flip == 3) {
        imgTrans.translate(width, height);
        imgTrans.scale(-1, -1);
        imgTrans.translate(-width, -height);
    }

    emit transformChanged();
}

void DrawParam::addTranslate(int dx, int dy)
{
    offsetX += dx;
    offsetY += dy;
    applyTransform();

}

void DrawParam::addScale(double ds)
{
    double dz = zoom*ds;
    zoom -= dz;
    double w = width * zoom;
    double h = height * zoom;
    if (w < rect.width()/10 || h < rect.height()/10)
        zoom += dz;
//    if (zoom < 0.1) zoom = 0.1;
    if (zoom > 20.0) zoom = 20.0;
    applyTransform();
}

void DrawParam::adjustWindow(double dx, double dy)
{
    curWW += dx;
    curWL -= dy;
    emit curWindowChanged();
}

void DrawParam::addAngle(double a)
{
    angle += a;
    int n = angle / 360;
    angle -= 360*n;
    applyTransform();
    emit angleChanged();
}

void DrawParam::addFlip(int type)
{
    int e = (flip & type) >> (type - 1);
    if (e) {
        flip = flip & ~type;
    } else {
        flip = flip | type;
    }

    applyTransform();
}

void DrawParam::rotate(double a)
{
    angle = a;
    emit angleChanged();
}


const QTransform &DrawParam::getTransform() const
{
    return transform;
}

void DrawParam::setTransform(const QTransform &newTransform)
{
    if (transform == newTransform)
        return;
    transform = newTransform;
    emit transformChanged();
}

const QTransform &DrawParam::getImgTrans() const
{
   return imgTrans;
}

int DrawParam::getFrame() const
{
    return frame;
}

void DrawParam::setFrame(int newFrame)
{
    if (frame == newFrame)
        return;
    frame = newFrame;
    emit frameChanged();
}

int DrawParam::getWidth() const
{
    return width;
}

void DrawParam::setSize(int newWidth, int newHeight)
{
    if (width == newWidth && height == newHeight)
        return;
    width = newWidth;
    height = newHeight;
    emit sizeChanged();
}

int DrawParam::getHeight() const
{
    return height;
}

double DrawParam::getDefWW() const
{
    return defWW;
}

double DrawParam::getDefWL() const
{
    return defWL;
}

void DrawParam::setDefWindow(double newDefWL, double newDefWW)
{
    if (qFuzzyCompare(defWL, newDefWL) && qFuzzyCompare(defWW, newDefWW))
        return;
    defWL = newDefWL;
    defWW = newDefWW;
    emit defWindowChanged();
}

double DrawParam::getCurWW() const
{
    return curWW;
}


double DrawParam::getCurWL() const
{
    return curWL;
}

void DrawParam::setCurWindow(double newCurWL, double newCurWW)
{
    if (qFuzzyCompare(curWL, newCurWL) && qFuzzyCompare(curWW, newCurWW))
        return;
    curWL = newCurWL;
    curWW = newCurWW;
    emit curWindowChanged();
}

double DrawParam::getZoom() const
{
    return zoom;
}

void DrawParam::setZoom(double newZoom)
{
    if (qFuzzyCompare(zoom, newZoom))
        return;
    zoom = newZoom;
    emit zoomChanged();
}

double DrawParam::getAngle() const
{
    return angle;
}

void DrawParam::setAngle(double newAngle)
{
    if (qFuzzyCompare(angle, newAngle))
        return;
    angle = newAngle;
    emit angleChanged();
}


bool DrawParam::getNegative() const
{
    return negative;
}

void DrawParam::setNegative(bool newNegative)
{
    if (negative == newNegative)
        return;
    negative = newNegative;
    emit negativeChanged();
}

